//
//  ComicsDetailViewController.swift
//  Spookiness
//
//  Created by 腾億 on 2017/12/14.
//  Copyright © 2017年 tml. All rights reserved.
//

import UIKit

protocol ComicViewWillEndDraggingDelegate: class {
    func comicWillEndDragging(_ scrollView: UIScrollView)
}

class ComicsDetailViewController: UIViewController {

    fileprivate var comicid: Int = 0
    
    fileprivate var pageContentView: SGPageContentView!
    fileprivate var titleView:SGPageTitleView!
    fileprivate var configure:SGPageTitleViewConfigure!
    
    fileprivate lazy var mainScrollView: UIScrollView = {
       let scrollView = UIScrollView()
        scrollView.delegate = self
        return scrollView
    }()
    
    fileprivate lazy var navigationBarY: CGFloat = {
        return navigationController?.navigationBar.frame.maxY ?? 0
    }()
    
    fileprivate lazy var headView: ComicsHead = {
       return ComicsHead(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: navigationBarY + 150))
    }()
    
    fileprivate lazy var detailVC: DetailViewController = {
        let vc = DetailViewController()
        vc.delegate = self
        return vc
    }()
    
    fileprivate lazy var commentVC: CommentViewController = {
        let vc = CommentViewController()
        vc.delegate = self
        return vc
    }()
    
    fileprivate lazy var chapterVC: ChapterViewController = {
       let vc = ChapterViewController()
        vc.delegate = self
        return vc
    }()
    
    private var detailStatic: DetailStaticModel?
    private var detailRealtime: DetailRealtimeModel?
    
    convenience init(comicid: Int) {
        self.init()
        self.comicid = comicid
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.isHidden = false
        //导航栏按钮颜色
        navigationController?.navigationBar.tintColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        //导航栏字体颜色以及大小
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white , NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17)]
        //状态栏颜色
        UIApplication.shared.statusBarStyle = .lightContent
        
        navigationController?.navigationBar.barStyle = .black
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        
        mainScrollView.contentOffset = CGPoint(x: 0, y: -mainScrollView.parallaxHeader.height)
        setUpUI()
        loadData()
        // Do any additional setup after loading the view.
    }
    
    fileprivate func  loadData() {
        
        let group = DispatchGroup()
        group.enter()
        NetworkTool.loadDetailStaticData(comicid: comicid) { [weak self] (detailStaticModel) in
            self?.detailStatic = detailStaticModel
            self?.headView.detailStatic = detailStaticModel.comic
            self?.detailVC.detailStatic = detailStaticModel
            self?.chapterVC.detailStatic = detailStaticModel
            //评论数据请求
            NetworkTool.loadCommentData(object_id: detailStaticModel.comic?.comic_id ?? 0, thread_id: detailStaticModel.comic?.thread_id ?? 0, page: -1, completionHandle: { (commentListModel) in
                self?.commentVC.commentList = commentListModel
                group.leave()
            })
        }
        
        group.enter()
        NetworkTool.loadDetailRealtimeData(comicid: comicid) { [weak self] (detailRealModel) in
            self?.detailRealtime = detailRealModel
            self?.headView.detailRealtime = detailRealModel.comic
            self?.detailVC.detailRealtime = detailRealModel
            self?.chapterVC.detailRealtime = detailRealModel
            group.leave()
        }
        
        group.enter()
        NetworkTool.loadLoveData(comicid: comicid) { [weak self] (loveModel) in
            self?.detailVC.guessLike = loveModel
            group.leave()
        }
        
        group.notify(queue: DispatchQueue.main) {
             self.detailVC.detailReloadData()
            self.chapterVC.chapterReloadData()
            self.commentVC.commentReloadData()
        }
        
    }
    
    fileprivate func setUpUI() {
        view.addSubview(mainScrollView)
        mainScrollView.snp.makeConstraints {
            $0.edges.equalTo(self.view.usnp.edges).priority(.low)
            $0.top.equalToSuperview()
        }
        
        //
        mainScrollView.parallaxHeader.view = headView
        mainScrollView.parallaxHeader.height = navigationBarY + 150
        mainScrollView.parallaxHeader.minimumHeight = navigationBarY
        mainScrollView.parallaxHeader.mode = .fill
        //
        configure = SGPageTitleViewConfigure.init()
        configure.titleColor = UIColor.black
        configure.titleSelectedColor = kRGBA(r: 127, g: 221, b: 146, a: 1)
        configure.indicatorScrollStyle = SGIndicatorScrollStyleHalf
        configure.indicatorColor = kRGBA(r: 127, g: 221, b: 146, a: 1)
        configure.titleFont = UIFont.systemFont(ofSize: 17)
        let titleArr = ["详情", "目录", "评论"]
        titleView = SGPageTitleView(frame: CGRect(x:0 , y:headView.y , width:self.view.width , height:kSCREEN_NAVBAR ), delegate: self, titleNames: titleArr, configure: configure)
        mainScrollView.addSubview(titleView)
        titleView.isShowIndicator = true//指示器开启
        titleView.isOpenTitleTextZoom = false//关闭文字缩放效果
        titleView.isShowBottomSeparator = true//开启底部分割线
        titleView.isTitleGradientEffect = false//关闭文字有渐变效果
        let contentViewHeight =  self.view.height - 2*kSCREEN_NAVBAR - kSCREEN_STAUSBAR
        let vcs = [detailVC, chapterVC, commentVC] as [Any]
        pageContentView = SGPageContentView(frame: CGRect(x:0 , y:titleView.frame.maxY , width:self.view.width , height:contentViewHeight ), parentVC: self, childVCs: vcs)
        pageContentView.delegatePageContentView = self
        mainScrollView.addSubview(pageContentView)
        
    }
    

}


extension ComicsDetailViewController : UIScrollViewDelegate , ComicViewWillEndDraggingDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.y >= -scrollView.parallaxHeader.minimumHeight {
            navigationController?.navigationBar.barStyle = .black
            navigationController?.navigationBar.setBackgroundImage(UIImage(named: "nav_bg"), for: .default)
            navigationController?.navigationBar.shadowImage = UIImage()
            navigationItem.title = detailStatic?.comic?.name
        } else {
            navigationController?.navigationBar.barStyle = .black
            navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
            navigationController?.navigationBar.shadowImage = UIImage()
            navigationItem.title = ""
        }
    }
    
    func comicWillEndDragging(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.y > 0 {
            mainScrollView.setContentOffset(CGPoint(x: 0,
                                                    y: -self.mainScrollView.parallaxHeader.minimumHeight),
                                            animated: true)
        } else if scrollView.contentOffset.y < 0 {
            mainScrollView.setContentOffset(CGPoint(x: 0,
                                                    y: -self.mainScrollView.parallaxHeader.height),
                                            animated: true)
        }
    }
    
}

extension ComicsDetailViewController: SGPageContentViewDelegate {
    func pageContentView(_ pageContentView: SGPageContentView!, progress: CGFloat, originalIndex: Int, targetIndex: Int) {
        self.titleView.setPageTitleViewWithProgress(progress, originalIndex: originalIndex, targetIndex: targetIndex)
    }
}

extension ComicsDetailViewController : SGPageTitleViewDelegate {
    func pageTitleView(_ pageTitleView: SGPageTitleView!, selectedIndex: Int) {
        
        self.pageContentView.setPageCententViewCurrentIndex(selectedIndex)
    }
    
    
}

